﻿@page "/dimensions/index"
@attribute [Authorize]

@inject IDimensionService DimensionService
@inject ISnackbar Snackbar
@inject IDialogService DialogService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService

<MudCard>
    <MudCardContent>
        <SearchTablePartial @ref="_requestTable" T="DimensionInfoResponseDto" ServerData="ServerReload"
                            SearchPartial="_searchPartial"
                            AddHandler="AddHandler"
                            EditHandler="EditHandler">

        </SearchTablePartial>
    </MudCardContent>
</MudCard>

@code {
    private DimensionSearchRequestDto _searchRequestDto = new();
    private IEnumerable<DimensionInfoResponseDto> _pagedData = new List<DimensionInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<DimensionInfoResponseDto> _requestTable = new();
    private int _totalItems;

    private DialogOptions _dialogOptions = new()
        {
            MaxWidth = MaxWidth.Medium,
            CloseButton = true,
            FullWidth = true,
            DisableBackdropClick = true
        };

    async Task SearchButtonClick()
    {
        await _requestTable.ReloadServerData();
    }

    async Task AddHandler()
    {
        var dialog = await DialogService.ShowAsync<DimensionEdit>("新增维度", _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            Snackbar.Clear();
            Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
            Snackbar.Add("保存成功", Severity.Success);
            await _requestTable.ReloadServerData();
        }
    }

    async Task EditHandler(DimensionInfoResponseDto model)
    {
        var parameters = new DialogParameters { { "Key", model.Key } };
        var dialog = await DialogService.ShowAsync<DimensionEdit>("修改数据库", parameters, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            Snackbar.Clear();
            Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
            Snackbar.Add("保存成功", Severity.Success);
            await _requestTable.ReloadServerData();
        }
    }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    private async Task<TableData<DimensionInfoResponseDto>> ServerReload(TableState state)
    {
        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var result = await DimensionService.QueryDimensionsByPage(_searchRequestDto);

        if (result is { Succeeded: true })
        {
            _pagedData = result.Data;
            _totalItems = result.TotalCount;
        }
        return new TableData<DimensionInfoResponseDto>() { TotalItems = _totalItems, Items = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        _searchPartial.IsAdd = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Dimension.CreateDimension))).Succeeded;
        _searchPartial.IsEdit = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Dimension.UpdateDimension))).Succeeded;
        _searchPartial.IsDelete = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Dimension.DeleteDimension))).Succeeded;
        //_searchPartial.IsImportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Database.ImportDatabase))).Succeeded;
        //_searchPartial.IsExportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Database.ExportDatabase))).Succeeded;
    }

}